Showing posts with label OSX. Show all posts
Showing posts with label OSX. Show all posts

Tuesday, December 27, 2016

How to compile a C++ program on Mac OSX

File "HelloWorld.cpp" contents between the dashed lines:

-----

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    cout << "Hello again!" << endl;
    return 0;
}

-----

Now compile the program:

g++ HelloWorld.cpp -o HelloWorld

Now run the program:

./HelloWorld


Wednesday, November 2, 2016

My .vimrc customizations on OS X (MacOS)

I have bits and pieces of this scattered throughout this blog.  This is an effort to put it into one place. Don't be surprised if I add more to this post later.

Helpful customizations for .vimrc:

" This turns off the annoying bell every time I hit the wrong key
set visualbell

" The following two lines turn on syntax highlighting
filetype plugin indent on
syntax on

" This tells vim that I am using a black background and need light colored text
set background=dark

Thursday, April 7, 2016

How to download and archive a website using wget on OSX

(This is how to get wget without installing homebrew or installing someone else's compiled code.  I just wanted to remind myself how to install from source.)

Install xcode from Apple.  I think the most recent versions of OSX have this built-in.  Mine did and I cannot remember if I installed it previously or not.

Initialize xcode so no weird errors happen when we call it later.  (Use Launcher to find xcode and run it.  Accept the EULA).

Install the xcode command line tools:

xcode-select --install

Thankfully, curl is already built-in.  So use curl to download wget:

cd ~/Downloads
curl -O http://ftp.gnu.org/gnu/wget/wget-1.15.tar.gz
tar -zxvf wget-1.15.tar.gz
cd wget-1.15/
./configure --without-ssl
make
sudo make install
cd ..
rm -rf ~/Downloads/wget*
wget

The line above simply tests wget.  Now test wget:

wget http://ftp.gnu.org/gnu/wget/wget-1.15.tar.gz

Now download the desired web site:

cd ~/Downloads
wget --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains www.whatever.com --no-parent www.whatever.com/.

Sources:
I followed these two pages but put my corrections in my instructions above.




Sunday, October 19, 2014

SpliX fixes Samsung ML 1740 on OS X Yosemite

Upgraded to OS X Yosemite.  First casualty = my Samsung ML 1740 printer.

Mavericks had not been kind to my printer, either.  I had previously gotten it to work with tech magic I've long forgotten - something about GhostScript and CUPS.  This time, I found a solution called SpliX.

SpliX installs CUPS drivers for Samsung printers.

----
Update 2015-06-12:

This only worked for a while.  It would sporadically fail.  I finally gave up and purchased a new Brother printer.

Saturday, October 18, 2014

Screen shots in OS X

Splat + Shift + 3 = Captures entire screen

Splat + Shift + 4 = Captures a custom rectangle (click and drag rectangle over desired area)

Splat + Shift + 4 then Space = Captures a window (click on the window)

Screen shots are saved to the desktop



Saturday, June 21, 2014

Mount Amazon EC2 instances on OS X with SSHFS

This gem lets you manipulate files on your remote EC2 instance as if they were local.  Think of it like a mini Dropbox near instantaneously updating files between your MacBook and your Ubuntu instance at Amazon.

Step 1: Configure SSH

Copy your Amazon .pem file to ~/.ssh
Change permissions on the .pem file:

chmod 600 ~/.ssh/myfile.pem

Test the .pem file:

ssh -i ~/.ssh/myfile.pem ubuntu@aws_instance_name.com

Store the .pem file in ssh so you can enter a shorter ssh command in the future:

ssh-add -K ~/.ssh/myfile.pem

Now you can ssh to your Ubuntu instance more easily:

ssh ubuntu@aws_instance_name.com

Step 2: Install SSHFS

Download and install both packages from http://osxfuse.github.io

OSXFUSE
SSHFS

Step 3: Configure the mount point

mkdir ~/AmazonEC2

sshfs ubuntu@aws_instance_name.com:/home/ubuntu/rails_projects ~/AmazonEC2

Now your EC2 files located at /home/ubuntu/rails_projects will be "synced" with ~/AmazonEC2

I do not think they are actually syncing - based on response times, I think file manipulations are done directly to the remote host.

For fun, take a look at mounted file systems:

df


Sunday, December 29, 2013

Better fonts and colors in OSX Terminal

Open a Terminal session.

Terminal | Preferences | Settings | Homebrew
Font | Change
English | Menlo | Regular | 14
Default

Saturday, December 28, 2013

Part 2: How to install Rails and MySQL in Mavericks OSX

[Continued from Part 1: How to install Rails and MySQL in Mavericks OSX]

In Part 1, we installed Xcode, Homebrew, RVM, MySQL, and upgraded to the latest stable release of Ruby.

In Part 2, we install Git, Github, Gems, and Rails.

Install and configure Git

brew install git

I followed my Git notes here


Github


Generate SSH keys

While following the guide above, I set up a repository at Github called project1 to match the local repository I had previously initialized.  The only purpose of "project1" was to set up and test Github connectivity.

Set up GitHub repository

cd to the project1 subdirectory and run the following commands:

touch hello.txt
git init
git add .
git commit -m "first commit"
git remote add origin git@github.com:<yourusername>/project1.git
git push -u origin master


Now that we know we can push to Github, we can remove the test files.  Remove project1 from both the local host and Github.

Gems


rvm install ruby
rvm use ruby --default
rvm rubygems current

This bumped me up to Ruby 2.1.0 and installed rubygems (among other things).

Rails

And - FINALLY - install Rails:

gem install rails




Friday, December 27, 2013

Part 1: How to install Rails and MySQL in Mavericks OSX


Install Rails

My buddy Mark Moser sent me to Andrew Kennedy's helpful blog so I could install Rails on my new MacBook (OSX Mavericks). My install did not quite follow Kennedy's documentation exactly so I've detailed below what works for me.

Install Xcode from the App store.

Run Xcode to initialize stuff.  I failed to do this the first time and I think it caused my Homebrew install (we do this later) to fail.

Install the command line tools:

xcode-select --install

Install Homebrew.  "Homebrew installs the stuff you need that Apple didn’t."  

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Install RVM.  RVM is Ruby Version Manager.  I think this lets me install and run multiple environments (Ruby versions and collections of gems).  The first time I tried to install rvm, the script complained and told me:

RVM PATH line not found for Bash, run the installer with '--auto-dotfiles' to fix it.

So I installed RVM like this:

curl -L https://get.rvm.io | bash -s stable --auto-dotfiles

The script gives this warning:

  * WARNING: You have '~/.profile' file, you might want to load it,
    to do that add the following line to '/Users/<username>/.bash_profile':

      source ~/.profile

I could not install the requirements (the next step) without sourcing the .profile (above).  

[Note to self: next time, insert this into my .bash_profile, exit the terminal, and then open a new terminal before proceeding.]

Install rvm requirements.  This figures out additional stuff needed and installs it - autoconf, automake, libtool, pkg-config, gcc46, libyaml, readline, libksba, openssl

rvm requirements

Note that this took a very, very long time and seemed stuck like this for an hour (crazy, right?) or so:

Installing required packages: autoconf, automake, libtool, pkg-config, gcc46, libyaml, readline, libksba, openssl........

The man page for rvm says this about requirements:

Show additional OS specific dependencies/requirements for building various rubies.

The thing that's weird about this is I think it installs these requirements; I don't think it only shows (displays) them.

Since we made changes, now we need to reload rvm:

rvm reload

Make sure we are running the most current stable version of rvm:

rvm get stable

Kennedy's blog guides us to install Ruby version 2.0.0.  I was already running ruby 2.0.0p247.  We can determine version info by running

ruby -v

I don't know if this version came with OSX or if I installed it during a previous session of tinkering.  Regardless, I went ahead and ran Kennedy's suggestion:

rvm install 2.0.0

RVM went out and found 2.0.0-p353 -- cool!  Notice it is slightly newer than the version I already had (p247).

I have no interest in legacy versions or enterprise edition, so I skipped those parts of Kennedy's blog.

Install mysql

Also, I chose to install mysql from MySQL.com at Mark's suggestion instead of installing it via Homebrew.

Get mysql from here to download the dmg

Open the disk image and run the mysql package.  I had to right-click and choose "Open" to get around the security feature that prevents some programs from running.

Mysql is now installed but not configured.  Time to do that.  I followed some of the documentation here and here

Add the mysql binaries to our environment path so bash can find them:

Insert the following line in ~/.bash_profile

export PATH=$PATH:/usr/local/mysql/bin

Exit the terminal session and open a new one.  The path environment variable is now set.

For the following commands to work, we need to change our directory:

cd /usr/local/mysql

Change user and group ownership so mysql is accessible to the mysql user.  Run these commands with sudo:

sudo chown -R mysql .
sudo chgrp -R mysql .

Start mysql:

sudo bin/mysqld_safe --user=mysql &

Any time you append an ampersand (&) to a bash line, you are telling bash to run this command indefinitely in the background.  So the above command runs a mysql daemon (service) called mysqld_safe as user "mysql" and runs it in the background.

This command tests mysql to see if it is running:

mysqladmin version

The system should respond with version information.

Gently shut down mysql:

bin/mysqladmin -u root shutdown

Restart mysql using the same command we used above:

sudo bin/mysqld_safe --user=mysql &

Make sure mysql is running.  The following command will display available databases:

mysqlshow

We now have mysql running but we are using default passwords.  Time to change that.  Connect to mysql as root:

mysql -u root

Note: the mysql "root" user is not the same as the operating system's "root" user. 

Display a list of users:

SELECT User, Host, Password FROM mysql.user;

System replies:

+------+-------------+----------+
| User | Host        | Password |
+------+-------------+----------+
| root | localhost   |          |
| root | myosx.local |          |
| root | 127.0.0.1   |          |
| root | ::1         |          |
|      | localhost   |          |
|      | myosx.local |          |
+------+-------------+----------+

Change the root password with the following command.  

UPDATE mysql.user SET Password = PASSWORD('YourNewPassword')
WHERE User = 'root';

Tell mysql to re-read grant (permission) tables so it will notice the password change:

FLUSH PRIVILEGES;

Confirm passwords were updated:

SELECT User, Host, Password FROM mysql.user;

The Password column will no longer be blank.

Sidenote: We could have changed the password by issuing four SET PASSWORD commands but I was lazy.  I list it here for documentation purposes - don't need to do this now:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'::1' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpwd');

Leave the mysql session:

exit

Now when we want to connect as root, we have to supply a password.  The command below starts up a mysql session as user "root" and tells the system to prompt us for a password:

mysql -u root -p

The documentation goes over removing anonymous accounts and securing test databases.  You can read that documentation here.  I am not following it because this is my play/dev machine and I want to leave these accounts as-is.  If you are connecting a system to a network, you should probably follow the documentation and remove them.

Now we have mysql running and configured, but we started it manually.  We now need to configure mysql to start automatically.

First, leave the mysql session and shut mysql down gently:

exit
bin/mysqladmin -u root -p shutdown

Notice this time we had to tell mysql to prompt for a password (the "-p" option).  Previously, we omitted that flag because there was no password assigned to root.

I followed the documentation here.  Open up the contents of the mysql disk image file that we previously downloaded.

Run MySQLStartupItem.pkg (right-click | Open)

The installer will guide you through the process.

Once done, look in the mysql disk image file one more time.  Run (right-click | Open) MySQL.prefPane.  This will install a MySQL utility in System Preferences.  Go there to start/stop MySQL and to tell MySQL whether or not to start automatically.


[To be continued in Part 2]

Tuesday, December 10, 2013

Mac OSX bash prompt

Put this in .bash_profile

export PS1="\w\$ "

This will considerably shorten your bash prompt to display only the working directory instead of the default which also displays computer name and user name.

We can display those as well by playing around with the defaults:

PS1='\h:\W \u\$ '

\h = First part of host name
\W = Directory name (notice this is uppercase rather than the lowercase I used above.  Lowercase w means the entire directory path; uppercase W means just the working directory)
\u = Username
\$ = If not root, display $ (otherwise display #)

There are other "magic" symbols that can be used - see http://www.ibm.com/developerworks/linux/library/l-tip-prompt/