Artificial intelligence is getting excited recently! I'm new to programming, but I wanted to use artificial intelligence, so I used python for the first time. It took a lot of time to build the environment on mac, so I summarized the points I did not understand. I hope it helps you build a python environment for artificial intelligence.
・ MacBook Pro (Retina, 13inch, Early 2015) · MacOS Sierra (version 10.12.2) ・ Processor (2.7 GHz Intel Core i5) · Memory (8 GB 1867 MHz DDR3) · Graphics (Intel Iris Graphics 6100 1536 MB)
In this article we will install three things. And for each, why install it? I will explain and post a reference article. ・ Installation of Homebrew ・ Installation of pyenv -Installation of anaconda (which contains python itself)
First of all, about the points to note when installing python and building the environment. There are two versions of python, the old version 2 series and the new version 3 series (and each of the 2 series and 3 series also has versions such as 2.x and 3.x). Normally, I think it's okay to use the new version 3 series, but that's not the case with python. This is because the 3rd system is gradually being used by many people, but there is no compatibility between the 2nd and 3rd systems, and there are still many libraries that only work with the 2nd system. Therefore, python does not install libraries etc. directly (in the global environment) on your personal computer, but creates and installs virtual environments separately for 2nd and 3rd systems. By the way, what is a virtual environment? A total of specifications and functions of a computer (virtual machine) virtually constructed by software on a computer. I think it's like creating a space in my computer that doesn't interfere with each other and developing there.
If you put the library directly on your computer, you will be wondering whether the version of python you are using is 2 or 3, and which library you have installed supports. I don't understand. Therefore, you should prepare a virtual environment that works with either, install the library separately by version, and manage it.
First of all, install a package management system called Homebrew. Homebrew makes it easy to install and remove software with a single command. Regarding Homebrew, the following sites are well organized and helpful. If you're not familiar with Homebrew, take a look.
"Who is homebrew? I investigated how it works." http://qiita.com/omega999/items/6f65217b81ad3fffe7e6
Now it's time to install Homebrew. To install Homebrew
・ An Intel CPU 1 ・ OS X 10.10 or higher 2 -Command Line Tools (CLT) for Xcode: xcode-select --install ・ A Bourne-compatible shell for installation (e.g. bash or zsh) 4 Seems to be needed.
The most common cause of installation errors is not having Command Line Tools installed. The following site is easy to understand about how to install Command Line Tools. If not, please read it.
"Install [mac] Command Line Tools" http://www.programing-style.com/mac/mac-command-line-tools-install/
When you're ready, start installing Homebrew.
homebrew installation command
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
You will be prompted to enter the password on the way. It will take some time.
Check if it was installed.
Confirmation of installation
$ brew doctor
If the installation is not successful, Warning will be displayed, but the following site was helpful.
"Procedures for installing Homebrew on Mac and how to solve Warnings" http://qiita.com/unsoluble_sugar/items/acaffa6d0e28c3c24934
Well, if you can install it successfully, I will update Homebrew just in case.
brew update
$ brew update
You can now keep Homebrew up to date.
pyenv is a tool for building a virtual environment of python and switching / managing versions. With this, you can easily switch between python 2 series and 3 series. Now let's install pyenv using Homebrew, which we installed earlier.
Install pyenv
$ brew install pyenv
I think that installing pyenv can be done easily with the above command, but after installing it, it was troublesome for me. pyenv has to go through environment variables, not just install it.
In addition to environment variables, you will also need a little knowledge of hidden files (.bash_profile) and vi commands to pass them.
Environment variables are a mechanism for storing settings related to application behavior, and are variables that store information required by the system.
"What is PATH? (Mac OS X)" http://qiita.com/soarflat/items/09be6ab9cd91d366bf71
Roughly speaking, ".bash_profile" is a file that is read when you start your computer and makes various settings. If you write environment variables here, it will be set so that you can use it. In your home directory (~), type ls –a to see all files, including hidden files one level below (file names with a'.' in front of them). You can see that there is a "bash_profile".
"What is .bash_profile? | An IT terminology dictionary that" I understand "and" I don't understand "but" I understand " http://wa3.i-3-i.info/word13650.html
I think it's okay to remember that the vi command is the command used here to change the ".bash_profile" in the terminal.
"What is vi [command]? | An IT terminology dictionary that makes you feel like you understand" but "do not understand" http://wa3.i-3-i.info/word11517.html
Now let's set the environment variables. In your home directory, type ls –a and make sure there is a ".bash_profile" in the hierarchy below.
.bash_Confirmation of profile location
$ ls -a
If you have ".bash_profile"
.bash_Enter profile
$ vi .bash_profile
You can only see the state just entered. This is where the vi command comes into play. While in the file
vi command
i
When you enter, you will be in input mode and you can enter new ones (make sure that the keyboard is in hiragana or katakana, so make sure it is in alphabetic characters). If you see --INSERT --at the end of the file, you are in input mode.
This is a newly added environment variable.
Newly added environment variables
export PATH="/usr/local/Cellar/.pyenv/shims:$PATH"
export PATH=${PYENV_ROOT}/bin:$PATH
eval "$(pyenv init -)"
You can enter it anywhere in the file. It's easy to understand, so I wonder if the last one is the best. After inputting, press the "esc key" to exit the input mode. Then save and exit .bash_profile.
Save and.bash_Exit profile
shift + zz
If you change ".bash_profile", be sure to reflect it.
Save changes
$ source .bash_profile
This article is very detailed on how to pass environment variables. "Understanding the setting of environment variables to pass the PATH (Mac OS X)" http://qiita.com/soarflat/items/d5015bec37f8a8254380
Once you have successfully set the environment variables, you can see a list of what you can install with pyenv.
List
pyenv install -l
This time, instead of installing python itself directly, in addition to python itself, install anaconda, which can install packages often used in python at once. It is very convenient because Numpy and Six, which are necessary for installing Chainer (a popular artificial intelligence library in Japan), are already installed in anaconda. It saves you the trouble of installing them separately and makes management easier. anaconda2-x.x.x is for 2 series and anaconda3-x.x.x is for 3 series.
Installation of anaconda2 system (latest is anaconda2-4.2.0 as of January 4, 2017)
Installation of anaconda2 system using pyenv
$ pyenv install anaconda2-4.2.0
Installation of anaconda3 series (latest is anaconda3-4.2.0 as of January 4, 2017)
Installation of anaconda3 system using pyenv
$ pyenv install anaconda3-4.2.0
You can probably install it without any problems. Check the version of python you are currently using with pyenv.
Check version
$ pyenv versions
What is displayed in the terminal
*system
anaconda2-4.2.0 (set by /Users/your_name/.pyenv/version)
anaconda3-4.2.0
The version marked with * is the version of python you are currently using. I think the default is system. Let's switch the version to python2 system.
Version change to 2 system
$ pyenv global anaconda2-4.2.0
Let's check if we could change it.
Check version
$ pyenv versions
What is displayed in the terminal
system
*anaconda2-4.2.0 (set by /Users/your_name/.pyenv/version)
anaconda3-4.2.0
If * attached to system is attached to anaconda2-4.2.0, it is successful. Be sure to update after changing the version.
Update pyenv
$ pyenv rehash
You can check the packages included in anaconda.
Check the package included in anaconda
$ conda list
You can see that Numpy and Six are included. Python is also included. I was able to install python safely.
virtualenv can build different environments with the same python version. pyenv is a python version control, virtualenv is a virtual environment management tool in the same python version. I didn't set virtualenv here, but if you are interested, please take a look.
"Virtual Environment Version Control Summary Python" http://qiita.com/hedgehoCrow/items/0733c63c690450b14dcf
"Building an environment with pyenv and virtualenv" http://qiita.com/Kodaira_/items/feadfef9add468e3a85b
Next time, I would like to install Chainer, an artificial intelligence library. If you have any mistakes, I would appreciate it if you could comment.
Recommended Posts